home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreosl / moreresources / moreresources.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.6 KB  |  134 lines

  1. /*
  2.     File:        MoreResources.c
  3.  
  4.     Contains:    Resource Manager utilities
  5.  
  6.     Written by:    Quinn
  7.  
  8.     Copyright:    Copyright © 2000 by Apple Computer, Inc., all rights reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <2>      3/9/00    gaw     API changes for MoreAppleEvents
  21.          <1>      6/3/00    Quinn   First checked in.
  22. */
  23.  
  24. /////////////////////////////////////////////////////////////////
  25.  
  26. // MoreIsBetter Setup
  27.  
  28. #include "MoreSetup.h"
  29.  
  30. // Mac OS Interfaces
  31.  
  32. #include <Resources.h>
  33.  
  34. // MIB Prototypes
  35.  
  36. // Our Prototypes
  37.  
  38. #include "MoreResources.h"
  39.  
  40. /////////////////////////////////////////////////////////////////
  41. /*
  42.  
  43.     Returns ResError() or, if it’s noErr and p is nil,
  44.     returns resNotFound.  The rationale for this routine
  45.     is that, under some documented circumstances, GetResource
  46.     can return nil but not set ResErr.
  47.  
  48.     pResHdl        input:    a resource handle
  49.  
  50.     RESULT CODES
  51.     ____________
  52.     Same as results for ResError
  53.     ____________
  54. */
  55.  
  56. pascal OSErr MoreResError(const void *pResHdl)
  57.     // See comment in interface part.
  58. {
  59.     OSErr err;
  60.     
  61.     err = ResError();
  62.     if ((err == noErr) && (pResHdl == nil)) {
  63.         err = resNotFound;
  64.     }
  65.     return err;
  66. }// end MoreResError
  67. /******************************************************************************
  68.     Given an FSSpec to a file, open it with write permission.
  69.     Check to make sure that the file reference returned actually has write
  70.     permission and return an error if it doesn't.
  71.  
  72.     pFSSpec        input:    The file to be opened.
  73.     pRefNum        output:    File reference for the newly opened file.
  74.     
  75.     RESULT CODES
  76.     ____________
  77.         Same as results for FSpOpenResFile()
  78.     ____________
  79. */
  80. pascal OSErr MoreResOpenResFileForWrite(const FSSpec *pFSSpec, SInt16 *pRefNum)
  81. {
  82.     OSErr        anErr = noErr;
  83.     
  84.     *pRefNum = FSpOpenResFile( pFSSpec, fsRdWrPerm );
  85.     if ( *pRefNum == -1 )
  86.     {
  87.         anErr = ResError();
  88.     }
  89.     else    //    Got it open, but can it be written to?
  90.     {
  91.         if ( ! MoreResIsResFileRefNumWritable( *pRefNum ) )
  92.         {
  93.             anErr = permErr;
  94.             CloseResFile( *pRefNum );
  95.             *pRefNum = -1;
  96.         }
  97.     }
  98.     
  99.     return anErr;
  100. }//end MoreResOpenResFileForWrite
  101. /******************************************************************************
  102.     Utility routine to check that a file opened with read/write permission
  103.     does in fact have read/write permission.
  104.     
  105.     It is possible to get a read-only file reference for a resource file that has
  106.     been opened with read/write permission.  This routine verify that a file open
  107.     with read/write permission does in fact have write permission.
  108.  
  109.     pRefNum        input:    File reference number for file to be checked.
  110.     
  111.     RESULT CODES
  112.     ____________
  113.         true        The resource file was really opened with write permission
  114.         false        It wasn't
  115.     ____________
  116. */
  117. pascal Boolean MoreResIsResFileRefNumWritable(const SInt16 pRefNum)
  118. {
  119.     FCBPBRec    fcbPB;
  120.     Boolean        result = false;
  121.  
  122.     fcbPB.ioNamePtr = nil;
  123.     fcbPB.ioVRefNum = 0;
  124.     fcbPB.ioRefNum = pRefNum;
  125.     fcbPB.ioFCBIndx = 0;
  126.     if ( PBGetFCBInfoSync( &fcbPB ) == noErr )
  127.     {
  128.         result = ( (fcbPB.ioFCBFlags & kioFCBWriteMask) != 0 );
  129.     }
  130.  
  131.     return result;
  132. }//end MoreResIsResFileRefNumWritable
  133. //******************************************************************************
  134.